package com.aiyi.education.admin.controller;


import com.aiyi.education.admin.service.IKsAdminPlanService;
import com.aiyi.education.admin.service.IKsAdminService;
import com.aiyi.education.entity.admin.KsAdmin;
import com.aiyi.education.entity.admin.KsAdminPlan;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.education.annotation.CurrentUser;
import com.education.annotation.Pass;
import com.education.annotation.ValidationParam;
import com.education.base.Constant;
import com.education.base.PublicResultConstant;
import com.education.config.ResponseHelper;
import com.education.config.ResponseModel;
import com.education.exception.BusinessException;
import com.education.util.ObjectUtil;
import com.education.util.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author renyong
 * @email sxgyry@163.com
 * @date
 */
@Slf4j
@RestController
@Api(tags = "管理员工作计划")
@RequestMapping("/ksAdminPlan")
public class KsAdminPlanController {

    @Autowired
    IKsAdminPlanService ksAdminPlanService;

    /**
     * 新增管理员工作计划
     * @param userObject
     * @param record
     * @date 2020年2月19日14:53:26
     * @return
     */
    @RequiresAuthentication
    @PostMapping("/addKsAdminPlan")
    public ResponseModel addKsAdminPlan(@CurrentUser Object userObject, @RequestBody KsAdminPlan record) {
        String loginType = ObjectUtil.getFieldValueByName("loginType", userObject).toString();
        //判断请求用户是否为管理员
        if (Constant.UserLoginType.ADMIN.equalsIgnoreCase(loginType)) {
            return ResponseHelper.validationFailure(PublicResultConstant.USER_NO_PERMITION);
        }
        Map<String, Object> map = new HashMap<>();
        try {
            map = ksAdminPlanService.addKsAdminPlan(map,userObject,record);
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                map.put("result", 400);
                map.put("msg", e.getMessage());
            } else {
                e.printStackTrace();
                return ResponseHelper.validationFailure(PublicResultConstant.FAILED);
            }
        }
        if ("400".equals(map.get("result").toString())) {
            return ResponseHelper.validationFailure(map.get("msg").toString());
        } else {
            return ResponseHelper.buildResponseModel(map.get("msg"));
        }
    }

    /**
     * 更新管理员工作计划
     * @param userObject
     * @param record
     * @date 2020年2月19日14:53:26
     * @return
     */
    @RequiresAuthentication
    @PostMapping("/updateKsAdminPlan")
    public ResponseModel updateKsAdminPlan(@CurrentUser Object userObject, @RequestBody KsAdminPlan record) {
        String loginType = ObjectUtil.getFieldValueByName("loginType", userObject).toString();
        //判断请求用户是否为管理员
        if (Constant.UserLoginType.ADMIN.equalsIgnoreCase(loginType)) {
            return ResponseHelper.validationFailure(PublicResultConstant.USER_NO_PERMITION);
        }
        Map<String, Object> map = new HashMap<>();
        try {
            map = ksAdminPlanService.updateKsAdminPlan(map,userObject,record);
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                map.put("result", 400);
                map.put("msg", e.getMessage());
            } else {
                e.printStackTrace();
                return ResponseHelper.validationFailure(PublicResultConstant.FAILED);
            }
        }
        if ("400".equals(map.get("result").toString())) {
            return ResponseHelper.validationFailure(map.get("msg").toString());
        } else {
            return ResponseHelper.buildResponseModel(map.get("msg"));
        }
    }

    /**
     * 删除管理员工作计划
     * @param userObject
     * @param record
     * @date 2020年2月19日14:53:26
     * @return
     */
    @RequiresAuthentication
    @PostMapping("/deleteKsAdminPlan")
    public ResponseModel deleteKsAdminPlan(@CurrentUser Object userObject, @RequestBody KsAdminPlan record) {
        String loginType = ObjectUtil.getFieldValueByName("loginType", userObject).toString();
        //判断请求用户是否为管理员
        if (Constant.UserLoginType.ADMIN.equalsIgnoreCase(loginType)) {
            return ResponseHelper.validationFailure(PublicResultConstant.USER_NO_PERMITION);
        }
        Map<String, Object> map = new HashMap<>();
        try {
            map = ksAdminPlanService.deleteKsAdminPlan(map,userObject,record);
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                map.put("result", 400);
                map.put("msg", e.getMessage());
            } else {
                e.printStackTrace();
                return ResponseHelper.validationFailure(PublicResultConstant.FAILED);
            }
        }
        if ("400".equals(map.get("result").toString())) {
            return ResponseHelper.validationFailure(map.get("msg").toString());
        } else {
            return ResponseHelper.buildResponseModel(map.get("msg"));
        }
    }

    /**
     * 根据条件查询管理员工作计划
     * @param userObject
     * @param requestJson
     * @date 2020年2月19日14:53:26
     * @return
     */
    @RequiresAuthentication
    @PostMapping("/searchKsAdminPlan")
    public ResponseModel searchKsAdminPlan(@CurrentUser Object userObject,
                                           @RequestParam(name = "pageIndex", defaultValue = "1", required = false) Integer pageIndex,
                                           @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                                           @RequestBody JSONObject requestJson) {
        String loginType = ObjectUtil.getFieldValueByName("loginType", userObject).toString();
        //判断请求用户是否为管理员
        if (Constant.UserLoginType.ADMIN.equalsIgnoreCase(loginType)) {
            return ResponseHelper.validationFailure(PublicResultConstant.USER_NO_PERMITION);
        }
        Map<String, Object> map = new HashMap<>();
        Page<KsAdminPlan> pageModel = new Page<>(pageIndex,pageSize);
        map = ksAdminPlanService.searchKsAdminPlan(map,userObject,pageModel,requestJson);
        if ("400".equals(map.get("result").toString())) {
            return ResponseHelper.validationFailure(map.get("msg").toString());
        } else {
            return ResponseHelper.buildResponseModel(map.get("msg"));
        }
    }

    /**
     * 根据主键查询
     * @param userObject
     * @param record
     * @date 2020年2月19日14:53:26
     * @return
     */
    @RequiresAuthentication
    @PostMapping("/selectByPrimaryKey")
    public ResponseModel selectByPrimaryKey(@CurrentUser Object userObject, @RequestBody KsAdminPlan record) {
        String loginType = ObjectUtil.getFieldValueByName("loginType", userObject).toString();
        //判断请求用户是否为管理员
        if (Constant.UserLoginType.ADMIN.equalsIgnoreCase(loginType)) {
            return ResponseHelper.validationFailure(PublicResultConstant.USER_NO_PERMITION);
        }
        if (record.getId()==null){
            return ResponseHelper.validationFailure(PublicResultConstant.PARAM_ERROR);
        }
        return ResponseHelper.buildResponseModel(ksAdminPlanService.selectByPrimaryKey(record.getId()));
    }
}
